package com.yanqu.road.server.cmd.union.cross;

import com.yanqu.road.entity.enums.eChannelType;
import com.yanqu.road.logic.config.ChannelConfig;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.union.UnionProto;
import com.yanqu.road.pb.union.cross.CrossUnionProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.thirdparty.PlatformMgr;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.ConfigHelper;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.utils.string.StringUtils;

@Cmd(code = Protocol.C_UNION_MODIFY, desc = "修改联盟信息")
public class CrossUnionModifyCmd extends CrossCommand {

    long playerId;
    int type;
    String content;
    String consume;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        playerId = packet.getPlayerId();
        CrossUnionProto.UnionModifyCrossReqMsg reqMsg = CrossUnionProto.UnionModifyCrossReqMsg.parseFrom(packet.getBytes());
        type = reqMsg.getType();
        content = reqMsg.getContent();
        consume = reqMsg.getConsume();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {

        int channelId = ConfigHelper.getInt("channelId");
        if((0 == type || 3 == type) && //改商会名或者改商会公告
                eChannelType.isMainLandChannel(channelId) && ChannelConfig.CN_FORBIDDEN_WORD_CHECK == 1) {//大陆渠道，实时检测内容是否屏蔽

            PlatformMgr.addCrossUnionModifyTask(serverId, playerId, type, content, consume);
        }else {

            ret = PlatformMgr.unionModify(serverId, playerId, type, content, consume);
        }

    }

    @Override
    public void sendData() {
        if(ret != 0){
            if(StringUtils.isNullOrEmpty(consume)) {
                UnionProto.UnionModifyRespMsg.Builder respMsg = UnionProto.UnionModifyRespMsg.newBuilder();
                respMsg.setRet(ret);
                MessageHelper.sendPacket(serverId, playerId, YanQuMessageUtils.buildMessage(Protocol.U_UNION_MODIFY, respMsg));
            }else {
                CrossUnionProto.UnionModifyCrossRespMsg.Builder respMsg = CrossUnionProto.UnionModifyCrossRespMsg.newBuilder();
                respMsg.setRet(ret);
                respMsg.setConsume(consume);
                MessageHelper.sendPacket(serverId, playerId, YanQuMessageUtils.buildMessage(Protocol.S_UNION_MODIFY_FROM_CROSS, respMsg));
            }
        }
    }
}
